import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

const activityNameObj = {
  "1": "砍价",
  "2": "拼团",
  "3": "一元拍卖"
};

class OrderGoods extends Base {
  constructor(goods, order) {
    super();

    this.order = order;
    this.orderNoSub = goods.orderNoSub;
    this.defId = goods.goodsDefId;
    this.modelId = goods.goodsModelId;
    this.name = goods.goodsName;
    this.modelName = goods.goodsModelName;
    this.pic = this.getOnePic(goods.goodsPic);
    this.sourcePrice = goods.goodsModelSourcePrice;
    this.count = goods.buyCount;
    this.payPrice = goods.shouldPayPrice;
    this.buyMethod = goods.buyMethod;
    this.status = goods.status;
    this.orderRefuseApplyId = goods.orderRefuseApplyId;
    this.isAppraise = goods.appraise === 1;

    this.setDatetimeData(goods, ['createTime', 'payTime', 'cancelTime', 'refuseTime']);

    let refusePrice = goods.refusePrice;
    if (SystemUtil.isEmpty(refusePrice)) {
      refusePrice = 0;
    } else {
      refusePrice = SystemUtil.roundValue(refusePrice / 100, 2);
    }
    this.refusePrice = refusePrice;

    let type = goods.sourceSub;
    if (['YYPM', 'KJ', 'PT'].indexOf(type) === -1) {
      this.price = goods.goodsModelSalePrice;
    } else {
      this.price = goods.shouldPayPrice;
    }
  }

  getApplyRetreatObject(reason) {
    let order = this.order;
    let postData = {
      orderNoMain: order.orderId,
      orderNoSub: this.orderNoSub,
      goodsDefId: this.defId,
      goodsModelId: this.modelId,
      branchId: order.branchId,
      applyRemark: reason
    };
    let partnerId = order.partnerId;
    if (partnerId !== null) {
      postData.partnerId = partnerId;
    }
    return postData;
  }
}

export default class GoodsOrderData extends Base {
  static reasonList = ['不想要了/拍错', '商品无法使用', '商品缺货', '协商一致退款', '商品和描述不符', '其他'];

  constructor(goodsList) {
    super();

    let obj = goodsList[0];
    let orderId = obj.orderNoMain;
    this.orderId = orderId;
    this.id = orderId;

    let preferentialPrice = obj.preferentialPrice;
    if (SystemUtil.isEmpty(preferentialPrice)) {
      preferentialPrice = 0;
    }
    this.preferentialPrice = preferentialPrice;

    this.branchId = obj.branchId;
    let partnerId = obj.partnerId;
    if (SystemUtil.isEmpty(partnerId)) {
      this.partnerId = null;
      this.shopName = obj.branchName;
    } else {
      this.partnerId = partnerId;
      this.shopName = obj.partnerName;
    }

    let type = obj.sourceSub;
    this.type = type;

    let activityTypeId = obj.activityTypeId;
    this.activityTypeId = activityTypeId;
    if (type === 'YDDD') {
      let activityName = activityNameObj[activityTypeId];
      if (activityName === undefined) {
        this.activityName = `${obj.goodsName} 预付款`;
      } else {
        this.activityName = `${activityName}-${obj.goodsName} 预付款`;
      }
    } else if (type === 'NOGOODS') {
      this.activityName = '门店支付';
    }

    if (['YDDD', 'NOGOODS'].indexOf(type) !== -1) {
      this.setDatetimeData(obj, ['createTime', 'payTime', 'cancelTime', 'refuseTime']);
      this.goodsList = null;

      this.status = obj.status;
      this.orderNoSub = obj.orderNoSub;
      let payPrice = obj.shouldPayPrice;
      this.totalPrice = SystemUtil.roundValue(payPrice - this.preferentialPrice, 2);

      let refusePrice = obj.refusePrice;
      if (SystemUtil.isEmpty(refusePrice)) {
        refusePrice = 0;
      } else {
        refusePrice = SystemUtil.roundValue(refusePrice / 100, 2);
      }
      this.refusePrice = refusePrice;
      return;
    }

    this.setGoodsList(goodsList);
  }

  setGoodsList(goodsData) {
    let goodsList = [];
    let totalPrice = 0,
      refusePrice = 0;
    let statusCount = {
      'status_1': 0,
      'status_2': 0,
      'status_3': 0
    };
    for (let i = 0; i < goodsData.length; i++) {
      let goods = goodsData[i];
      goods = new OrderGoods(goods, this);

      let status = goods.status;
      if ([1, 2, 3].indexOf(status) !== -1) {
        statusCount[`status_${status}`]++;
      }

      totalPrice += goods.payPrice;
      refusePrice += goods.refusePrice;
      goodsList.push(goods);
    }
    this.goodsList = goodsList;
    this.totalPrice = SystemUtil.roundValue(totalPrice - this.preferentialPrice, 2);
    this.refusePrice = SystemUtil.roundValue(refusePrice, 2);

    if (goodsList.length === 1) {
      this.status = goodsList[0].status;
      return;
    }

    if (statusCount.status_1 !== 0) {
      this.status = 1;
    } else if (statusCount.status_2 !== 0 && statusCount.status_3 !== 0) {
      this.status = 1;
    } else {
      this.status = goodsList[0].status;
    }
  }

  getShopData() {
    let partnerId = this.partnerId;
    if (partnerId !== null) {
      return {
        partnerId
      };
    }

    return {
      branchId: this.branchId
    };
  }

  getApplyRetreatObject(reason) {
    let postData = {
      orderNoMain: this.orderId,
      orderNoSub: this.orderNoSub,
      branchId: this.branchId,
      applyRemark: reason
    };
    let partnerId = this.partnerId;
    if (partnerId !== null) {
      postData.partnerId = partnerId;
    }
    return postData;
  }
}
